package render

import (
	"gitee.com/youbeiwuhuan/gin-practice/gin-simple-zap/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
)

func ReturnJson(context *gin.Context, httpCode int, respData RespData) {

	context.JSON(httpCode, respData)
}

func ReturnError(context *gin.Context, msg string, data interface{}) {
	ReturnJson(context, http.StatusInternalServerError, ErrResp(msg, data))
}

// ReturnValidErr  参数校验错误
func ReturnValidErr(context *gin.Context, err error) {
	errs, ok := err.(validator.ValidationErrors)
	if !ok {
		// 非validator.ValidationErrors类型错误直接返回
		ReturnJson(context, http.StatusInternalServerError, ValidError(err.Error()))
		return
	}
	// 逐条翻译
	s := ""
	for _, e := range errs {
		//fmt.Println(e.Translate(valid.Trans))
		s = s + e.Translate(valid.Trans)
	}

	ReturnJson(context, http.StatusOK, ValidError(s))
}

// OK 成功返回
func OK(context *gin.Context, data interface{}) {
	ReturnJson(context, http.StatusOK, OkResp(data))
}

// ReturnNeedLogin 请登录
func ReturnNeedLogin(context *gin.Context) {
	ReturnJson(context, http.StatusOK, NoLogin("未登录"))
}

// ReturnAuthFail 无权访问
func ReturnAuthFail(context *gin.Context) {
	ReturnJson(context, http.StatusOK, NoAuth("无权访问"))
}
